package com.dexcoder.demo.quartz;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dexcoder.demo.model.ScheduleJob;
import com.dexcoder.demo.vo.ScheduleJobVo;

/**
 * 同步的任务工厂类
 *
 * Created by liyd on 12/19/14.
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class JobSyncFactory implements Job {

//    Long time;
//
//    public void setTime(Long time) {
//        this.time = time;
//    }

    /* 日志对象 */
    private static final Logger LOG = LoggerFactory.getLogger(JobSyncFactory.class);

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        LOG.info("JobSyncFactory execute");
        JobKey key = jobExecutionContext.getJobDetail().getKey();
        JobDataMap mergedJobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
//        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        ScheduleJob scheduleJob = (ScheduleJob) mergedJobDataMap.get(ScheduleJobVo.JOB_PARAM_KEY);
        long time2 = System.currentTimeMillis();
        long time = mergedJobDataMap.getLong("time");
        mergedJobDataMap.put("time",time2);
        System.out.println("instance:" + key + " jobName:" + scheduleJob.getJobName() + "  间隔" + (time2-time) + "ms");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
